<?php
namespace library;
//use library\ArrayMethods as ArrayMethods;
//use library\StringMethods as StringMethods;
/**
 * Description of Inspector
 *
 * @author Damian
 */
class Inspector {
  
    protected $class;
    protected $meta ;
    protected $properties;
    protected $methods;
    private $reflection;
    public function __construct($class) {
        $this->class=$class;
        
    }
    protected function _getClassComment(){
        $reflection = new \ReflectionClass($this->class);
        return $reflection->getDocComment();
    }
    protected function _getClassProperties(){
        $reflection = new \ReflectionClass($this->class);
        return $reflection->getProperties();
    }
    public function propertyExists($property){
        $properties = $this->getClassProperties();
        return in_array($property, $properties);
    }

    protected function _getClassMethods(){
        $reflection = new \ReflectionClass($this->class);
        return $reflection->getMethods();
    }
    protected function _getPropertyComment($property){
        try {
            $reflection = new \ReflectionProperty($this->class,  lcfirst($property));
            return $reflection->getDocComment();
        } catch (\Exception $ex) {
            try{
                $reflection = new \ReflectionProperty($this->class,  ucfirst($property));
                return $reflection->getDocComment();
            }catch (\Exception $e) {
                return null;
            }            
        }      
    }
    protected function _getMethodComment($method){
        $reflection = new \ReflectionMethod($this->class,$method);
        return $reflection->getDocComment();
    }
    protected function _parse($comment){
        $meta = array();
        $pattern = "(@[a-zA-Z]+\s*[a-zA-Z0-9".StringMethods::POLISH.", ()_]*)";
        $matches = StringMethods::match($comment, $pattern);
        if ($matches!=null){
            foreach ($matches as $match) {
                $parts = ArrayMethods::clean(
                ArrayMethods::trim(
                StringMethods::split($match, "[\s]",2)));                
           $meta[$parts[0]] = true;
           if (sizeof($parts)>1){
               $meta[$parts[0]] = ArrayMethods::clean(
                ArrayMethods::trim(
                StringMethods::split($parts[1],",")));  
           }
                }
        }
        return $meta;
    }
    public function getClassMeta() {
        if (!isset($this->meta["class"])) {
            $comment = $this->_getClassComment();
            if (!empty($comment)) {
                $this->meta["class"] = $this->_parse($comment);
            } else {
                $this->meta["class"] = null;
            }
        }
        return $this->meta["class"];
    }
    public function getClassProperties(){
            $this->properties=  $this->_getClassProperties();
            foreach ($this->properties as $property){
                $properties[]=$property->getName();
                $this->properties[] = $property->getName();
            }
        return $properties;
    }
    public function getClassMethods(){
        if(!isset($this->methods)){
            $this->methods= $this->_getClassMethods();
            foreach ($this->methods as $method) {
                $this->methods[] = $method->getName();                
            }
        }
        return $this->methods;
    }
    public function getPropertyMeta($property) {
        if (!isset($this->meta["properties"][$property])) {
            $comment = $this->_getPropertyComment($property);
            if (!empty($comment)) {
                $this->meta["class"][$property] = $this->_parse($comment);
            } else {
                $this->meta["class"][$property] = null;
            }
        }
        return $this->meta["class"][$property];
    }
    public function getMethodyMeta($method) {
        if (!isset($this->meta["methods"][$method])) {
            $comment = $this->_getMethodComment($method);
            if (!empty($comment)) {
                $this->meta["methods"][$method] = $this->_parse($comment);
            } else {
                $this->meta["methods"][$method] = null;
            }
        }
        return $this->meta["methods"][$method];
    }
}